package org.example.bussiness;

import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.engine.ManagementService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.springframework.stereotype.Service;

/**
 * @author hsj
 * @description:
 * @date 2023/2/15 15:53
 */
@Service
@RequiredArgsConstructor
public class WfExpressionServiceImpl implements IWfExpressionService {

    private final ProcessEngineConfigurationImpl processEngineConfiguration;
    private final ManagementService managementService;
    private final RuntimeService runtimeService;

    @Override
    public Object getValue(String processInstanceId, String exp) {
        Expression expression = processEngineConfiguration.getExpressionManager().createExpression(exp);
        return managementService.executeCommand((commandContext) ->
                expression.getValue((ExecutionEntity) runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult()));
    }

}
